//
//  ViewController.m
//  002-KVC取值&赋值过程
//
//  Created by Cooci on 2019/12/7.
//  Copyright © 2019 Cooci. All rights reserved.
//

#import "ViewController.h"
#import "LGPerson.h"
#import <AVKit/AVKit.h>

@interface ViewController ()

@property (nonatomic, strong) AVPlayer *player;

@end

@implementation ViewController

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    /*
     AVPlayerTimeControlStatusPaused,
     AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate,
     AVPlayerTimeControlStatusPlaying
     */
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
        switch (status) {
            case AVPlayerStatusUnknown:
                NSLog(@"KVO：未知状态，此时不能播放");
                break;
            case AVPlayerStatusReadyToPlay:
            {
                if (self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying) {
                    
                }
                NSLog(@"KVO：准备完毕，可以播放   %ld++++++%@",(long)self.player.timeControlStatus,self.player.reasonForWaitingToPlay);
            }
                break;
            case AVPlayerStatusFailed:
                NSLog(@"KVO：加载失败，网络或者服务器出现问题");
                break;
            default:
                break;
        }
    }
    
    if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
        NSArray * array = self.player.currentItem.loadedTimeRanges;
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue]; //本次缓冲的时间范围
        NSTimeInterval totalBuffer = CMTimeGetSeconds(timeRange.start) + CMTimeGetSeconds(timeRange.duration); //缓冲总长度
        NSLog(@"共缓冲%.2f",totalBuffer);
    }
    
}
 
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    
    NSString *str = @"https://webtest.chinamcloud.cn/cms/rmt3676//upload/Audio/2021/07/27/c9a75f25adbc4b98988f1f8d126a31dc.mp3";
//    str = @"https://video.webtest.chinamcloud.cn/crms/media/vod/video/2021-07-22/A4C49BF2-3A89-6DCA-DC2B-64A08CB95827/A4C49BF2-3A89-6DCA-DC2B-64A08CB95827_128k.mp3";
    
//    NSURL *url = [NSURL URLWithString:str];
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//    [item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
//
//    self.player = [AVPlayer playerWithPlayerItem:item];
//    [self.player play];
     
    UIImageView *img = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
    [self.view addSubview:img];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://upload-images.jianshu.io/upload_images/1694726-af525d66164420a7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            img.image = [UIImage imageWithData:data];
        });
    });
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    LGPerson *person = [[LGPerson alloc] init];
//    person->_isName = @"CD_isName";
//    person->name = @"CDname";
//    person->isName = @"CDisName";
//    person->_name = @"CD_name";
    
    
    //1、获取值
//    id v = [person valueForKey:@"name"];
//
//    NSLog(@"name = '%@'", v);
//
//    person.mun = 1234;
//    person.sex = true;
//    person.hei = @(188.8);
//    person.point = CGPointMake(10, 20);
//
////    [person setValue:@(1234) forKey:@"num"];
////    [person setValue:@(true) forKey:@"sex"];
////    [person setValue:@(188.88) forKey:@"hei"];
////    CGPoint p = CGPointMake(10, 20);
////    NSValue *vale = [[NSValue alloc]initWithBytes:&p objCType:"point"];
////    [person setValue:vale forKey:@"point"];
//
//    id num = [person valueForKey:@"num"];
//    id sex = [person valueForKey:@"sex"];
//    id hei = [person valueForKey:@"hei"];
//    id point = [person valueForKey:@"point"];
//
//    NSLog(@"num = %@", num);
//    NSLog(@"sex = %@", sex);
//    NSLog(@"hei = %@", hei);
//    NSLog(@"point = %@", point);
//
//
//    [NSNumber numberWithInt:1];
    
    
    //2、设置值
    [person setValue:@"6666" forKey:@"name"];
     
    
    // 1: KVC - 设置值的过程 setValue 分析调用过程
    // [person setValue:@"LG_Cooci" forKey:@"name"];

//     NSLog(@"%@-%@-%@-%@",person->_name,person->_isName,person->name,person->isName);
//     NSLog(@"%@-%@-%@",person->_isName,person->name,person->isName);
//     NSLog(@"%@-%@",person->name,person->isName);
//     NSLog(@"%@",person->isName);
    
    
    NSLog(@"");
    
    // setter - getter  - KVC 设值 和 取值的流程
    // 2: KVC - 取值的过程
    // _name
//    person->_name = @"_name";
//    person->_isName = @"_isName";
//    person->name = @"name";
//    person->isName = @"isName";

//    NSLog(@"取值:%@",[person valueForKey:@"name"]);
    
    
    /**
     1: setname _
     2: 间接访问其他的变量
     
     1: getter .....
     2: 集合
     3: 间接
     4: 变量
     
     自定义一下KVC
     */

    [self arraysAndSet];
    
}

//MARK: - 集合类型的KVC 注意

- (void)arraysAndSet{
    
    LGPerson *person = [[LGPerson alloc] init];
    // 3: KVC - 集合类型
    person.arr = @[@"pen0", @"pen1", @"pen2", @"pen3"];
    NSArray *array = [person valueForKey:@"pens"];
    NSLog(@"%@",[array objectAtIndex:1]);
    NSLog(@"%@",[array objectsAtIndexes:[NSIndexSet indexSetWithIndex:2]]);
    NSLog(@"%d",[array containsObject:@"pen1"]);
    
    // set 集合
    person.set = [NSSet setWithArray:person.arr];
    NSSet *set = [person valueForKey:@"books"];
    [set enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"set遍历 %@",obj);
    }];
}


@end
